package com.codechuan.hallo_internal.ui.dashboard;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.fastjson.JSONObject;
import com.codechuan.hallo_internal.common.Constant;
import com.codechuan.hallo_internal.databinding.FragmentDashboardBinding;
import com.codechuan.hallo_internal.entity.SuccessOrder;
import com.codechuan.hallo_internal.ui.myOrder.MyOrder;
import com.codechuan.hallo_internal.utils.ContextUtil;
import com.codechuan.hallo_internal.utils.DialogUtil;
import com.codechuan.hallo_internal.utils.JDBCUtil;
import com.codechuan.hallo_internal.utils.MyProjectHttpUtil;

import java.util.ArrayList;

public class DashboardFragment extends Fragment {

    private FragmentDashboardBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        DialogUtil.showDialog(ContextUtil.context, "获取中请稍后~");
        // 获取订单列表
        Constant.executorService.execute(() -> {
            JSONObject orderList =  MyProjectHttpUtil.getUserOrderList();
            if (orderList.getString("code").equals("200")) {
                String string = orderList.getString("data");
                if (string.equals("[]")) {
                    Constant.ORDER_LIST = new ArrayList<>();
                    Constant.handler.post(()->{
                        DialogUtil.showDialog(ContextUtil.context,"暂无订单...");
                    });
                }else {
                    JSONObject data = JSONObject.parseObject(string);
                    Constant.ORDER_LIST = (ArrayList<SuccessOrder>) data.parseArray(data.getString("orderList"), SuccessOrder.class);
                }
            }

            Constant.handler.post(() -> {
                DialogUtil.showDialog(ContextUtil.context, "获取完成~");
                Context context = getContext();
                if(context!=null){
                    Intent intent = new Intent();
                    intent.setClass(context, MyOrder.class);
                    context.startActivity(intent);
                }
            });
        });
        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}